#!/bin/bash

debug_flag=0
val=0
value=0

byte_hex_test()
{
	if ! [[ $1  =~ ^[0-9A-Fa-f]{2}$ ]] ; then
		return 0
	else
		return 1
	fi
}

byte_read(){
	data=$(echo $val | awk '{print $'"$1"'}')
	byte_hex_test $data
	if [ $? -eq 0 ]; then
		return 0
	fi
	data=$(printf "%d" 0x$data)
	value=$data
	return 1

}
add_byte(){
	hi_byte=$(echo $val | awk '{print $'"$1"'}')
	lo_byte=$(echo $val | awk '{print $'"$2"'}')
	byte_hex_test $hi_byte
	if [ $? -eq 0 ]; then
		return 0
	fi
	byte_hex_test $lo_byte
	if [ $? -eq 0 ]; then
		return 0
	fi
	hi_byte=$(printf "%d" 0x$hi_byte)
	lo_byte=$(printf "%d" 0x$lo_byte)
	((hi_byte <<= 8))
	value=$((($lo_byte+$hi_byte)))
	return 1
}
# string_read [start byte] [length]
string_read(){
	value=""
	len=$2
	s=$(($1))
	e=$(($1 + len - 1))
	for i in $(seq $s $e)
	do
		data=$(echo $val | awk '{print $'"$i"'}')
		byte_hex_test $data
		if [ $? -eq 0 ]; then
			return 0
		fi
		data=$(printf "%d" 0x$data)
		if [ $data -ne 0 ];then
			value=$value$(printf "$(printf '\\x%x' $data)")
		else
			data=0x20
			value=$value$(printf "$(printf '\\x%x' $data)")
		fi
	done
	return 1
}
#$1=dev_addr, $2=reg, $3=val
get_PSU_information()
{
	val=$(ipmitool raw 0x30 0x89 2>>/dev/null)
	if [ -e /sys/bus/i2c/devices/0-005a/vendor ] && [ -e /sys/bus/i2c/devices/0-0059/vendor ];then
		#PSU1 Vin
		add_byte 1 2
		if [ $? -eq 1 ];then
			value=$(($value * 10))
			echo $value > /sys/bus/i2c/devices/0-005a/in1_input
		fi
		#PSU2 Vin
		add_byte 3 4
		if [ $? -eq 1 ];then
			value=$(($value * 10))
			echo $value > /sys/bus/i2c/devices/0-0059/in1_input
		fi
		#PSU 1 Vout
		add_byte 5 6
		if [ $? -eq 1 ];then
			value=$(($value * 10))
			echo $value > /sys/bus/i2c/devices/0-005a/in2_input
		fi
		#PSU 2 Vout
		add_byte 7 8
		if [ $? -eq 1 ];then
			value=$(($value * 10))
			echo $value > /sys/bus/i2c/devices/0-0059/in2_input
		fi
		#PSU 1 Iin
		add_byte 9 10
		if [ $? -eq 1 ];then
			value=$(($value * 10))
			echo $value > /sys/bus/i2c/devices/0-005a/curr1_input
		fi
		#PSU 2 Iin
		add_byte 11 12
		if [ $? -eq 1 ];then
			value=$(($value * 10))
			echo $value > /sys/bus/i2c/devices/0-0059/curr1_input
		fi
		#PSU 1 Iout
		add_byte 13 14
		if [ $? -eq 1 ];then
			value=$(($value * 10))
			echo $value > /sys/bus/i2c/devices/0-005a/curr2_input
		fi
		#PSU 2 Iout
		add_byte 15 16
		if [ $? -eq 1 ];then
			value=$(($value * 10))
			echo $value > /sys/bus/i2c/devices/0-0059/curr2_input
		fi
		#PSU 1 Pin
		add_byte 17 18
		if [ $? -eq 1 ];then
			value=$(($value * 1000000))
			echo $value > /sys/bus/i2c/devices/0-005a/power1_input
		fi
		#PSU 2 Pin
		add_byte 19 20
		if [ $? -eq 1 ];then
			value=$(($value * 1000000))
			echo $value > /sys/bus/i2c/devices/0-0059/power1_input
		fi
		#PSU 1 Pout
		add_byte 21 22
		if [ $? -eq 1 ];then
			value=$(($value * 1000000))
			echo $value > /sys/bus/i2c/devices/0-005a/power2_input
		fi
		#PSU 2 Pout
		add_byte 23 24
		if [ $? -eq 1 ];then
			value=$(($value * 1000000))
			echo $value > /sys/bus/i2c/devices/0-0059/power2_input
		fi
		#PSU 1 Temp1
		byte_read 25
		if [ $? -eq 1 ];then
			value=$(($value * 1000))
			echo $value > /sys/bus/i2c/devices/0-005a/temp1_input
		fi
		#PSU 2 Temp1
		byte_read 26
		if [ $? -eq 1 ];then
			value=$(($value * 1000))
			echo $value > /sys/bus/i2c/devices/0-0059/temp1_input
		fi

		byte_read 31
		if [ $? -eq 1 ];then
			echo $value > /sys/bus/i2c/devices/0-005a/present
		fi

		byte_read 32
		if [ $? -eq 1 ];then
			echo $value > /sys/bus/i2c/devices/0-0059/present
		fi

		#PSU 1 2 PSU power good
		byte_read 33
		if [ $? -eq 1 ];then
			echo $value > /sys/bus/i2c/devices/0-005a/power_good
		fi

		byte_read 34
		if [ $? -eq 1 ];then
			echo $value > /sys/bus/i2c/devices/0-0059/power_good
		fi

		add_byte 35 36
		if [ $? -eq 1 ];then
			echo $value > /sys/bus/i2c/devices/0-005a/fault
		fi

		add_byte 37 38
		if [ $? -eq 1 ];then
			echo $value > /sys/bus/i2c/devices/0-0059/fault
		fi

		byte_read 41
		if [ $? -eq 1 ];then
			echo $value > /sys/bus/i2c/devices/0-005a/pwm
		fi

		byte_read 42
		if [ $? -eq 1 ];then
			echo $value > /sys/bus/i2c/devices/0-0059/pwm
		fi

	fi

	#____________________________________________________________________________

	val=$(ipmitool raw 0x30 0x8a 2>>/dev/null)
	if [ -e /sys/bus/i2c/devices/0-005a/vendor ] && [ -e /sys/bus/i2c/devices/0-0059/vendor ];then
		#PSU1 mfr_ID
		string_read 1 20
		if [ $? -eq 1 ];then
			echo $value > /sys/bus/i2c/devices/0-005a/vendor
		fi
		#PSU2 mfr_ID
		string_read 21 20
		if [ $? -eq 1 ];then
			echo $value > /sys/bus/i2c/devices/0-0059/vendor
		fi
		#PSU1 mfr_model
		string_read 41 20
		if [ $? -eq 1 ];then
			echo $value > /sys/bus/i2c/devices/0-005a/model
		fi
		#PSU2 mfr_model
		string_read 61 20
		if [ $? -eq 1 ];then
			echo $value > /sys/bus/i2c/devices/0-0059/model
		fi
		#PSU1 mfr_serial
		string_read 81 30
		if [ $? -eq 1 ];then
			echo $value > /sys/bus/i2c/devices/0-005a/sn
		fi
		#PSU2 mfr_serial
		string_read 111 30
		if [ $? -eq 1 ];then
			echo $value > /sys/bus/i2c/devices/0-0059/sn
		fi

		#PSU1 mfr_rev
		string_read 141 2
		if [ $? -eq 1 ];then
			echo $value > /sys/bus/i2c/devices/0-005a/rev
		fi
		#PSU2 mfr_serial
		string_read 143 2
		if [ $? -eq 1 ];then
			echo $value > /sys/bus/i2c/devices/0-0059/rev
		fi
	fi
}

#$1=dev_addr, $2=reg, $3=len

PSU_monitor()
{

	while true
	do

		get_PSU_information
		if [ $debug_flag -eq 1 ];then
			bmc_fan_speed_ctrl
		fi

		sleep 1
	done
}

PSU_monitor
